package com.matace.keychain.presentation.view;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.matace.keychain.R;
import com.matace.keychain.data.RegisterRepositoryImpl;
import com.matace.keychain.domain.RegisterUseCase;
import com.matace.keychain.presentation.viewmodel.RegisterViewModel;



/**
 * 注册活动
 * 负责用户注册界面的显示和交互逻辑
 */
public class RegisterActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private EditText editTextConfirmPassword;
    private Button buttonRegister;
    private TextView textViewLoginPrompt;
    private RegisterViewModel registerViewModel;

    /**
     * 活动创建时调用
     * 初始化视图、ViewModel和事件监听器
     * @param savedInstanceState 保存的实例状态
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe_user);

        // 初始化视图
        editTextUsername = findViewById(R.id.editTextUsernameReg);
        editTextEmail = findViewById(R.id.editTextEmailReg);
        editTextPassword = findViewById(R.id.editTextPasswordReg);
        editTextConfirmPassword = findViewById(R.id.editTextConfirmPasswordReg);
        buttonRegister = findViewById(R.id.buttonRegister);
        textViewLoginPrompt = findViewById(R.id.textViewLoginPrompt);

        // 初始化ViewModel
        registerViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
            @Override
            public <T extends androidx.lifecycle.ViewModel> T create(Class<T> modelClass) {
                if (modelClass.isAssignableFrom(RegisterViewModel.class)) {
                    return (T) new RegisterViewModel(new RegisterUseCase(new RegisterRepositoryImpl(RegisterActivity.this)));
                }
                throw new IllegalArgumentException("Unknown ViewModel class");
            }
        }).get(RegisterViewModel.class);

        // 观察ViewModel中的数据变化
        registerViewModel.getUsername().observe(this, s -> editTextUsername.setText(s));
        registerViewModel.getEmail().observe(this, s -> editTextEmail.setText(s));
        registerViewModel.getPassword().observe(this, s -> editTextPassword.setText(s));
        registerViewModel.getConfirmPassword().observe(this, s -> editTextConfirmPassword.setText(s));
        registerViewModel.getError().observe(this, error -> {
            if (!TextUtils.isEmpty(error)) {
                Toast.makeText(RegisterActivity.this, error, Toast.LENGTH_SHORT).show();
            }
        });
        registerViewModel.getRegisterSuccess().observe(this, success -> {
            if (success) {
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

        // 设置点击事件监听器
        buttonRegister.setOnClickListener(v -> {
            registerViewModel.setUsername(editTextUsername.getText().toString());
            registerViewModel.setEmail(editTextEmail.getText().toString());
            registerViewModel.setPassword(editTextPassword.getText().toString());
            registerViewModel.setConfirmPassword(editTextConfirmPassword.getText().toString());
            registerViewModel.register();
        });

        textViewLoginPrompt.setOnClickListener(v -> {
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        });
    }
}